<?php
    /**
    * Edit Real Estate Project
    * @author trungdt
    * @since June 23, 2011
    * 
    * Change logs
    * 
    */

    if (!defined('EXPONENT')) exit('');

    $lang=exponent_lang_loadFile("modules/bdsmodule/default.php");
    
    /**
    * This function return combobox array by reading array $lang
    * 
    * @param mixed $lang Should be passed by variable reference
    * @param mixed $key Key name
    */
    function _getCombobox(&$lang, $key){
        $temp=array();
        for ($i=0;$i<$lang[$key];$i++)
        {
            $temp[$i]=$lang["{$i}{$key}"];
        }
        return $temp;
    }
    /**
    * Return array with item lineared
    * 
    * @param mixed $max
    */
    function _getComboboxLinear($max){
        $temp=array();
        for ($i=0;$i<$max;$i++)
            $temp[$i]=$i;
        return $temp;
    }
    
    // permission checking
    $have_permission=false;
    if (exponent_users_isAdmin() || exponent_permissions_check('administrate',$loc) || exponent_permissions_check('create',$loc) || exponent_permissions_check('edit',$loc))
    {
        $have_permission=true;
    }

    $bds = null;
    $config=null;
    if (isset($_GET['id'])) {
        $bds = $db->selectObject('bds','id='.intval($_GET['id']));
        if ($bds) {
            $loc = unserialize($bds->location_data);
        }
    }

    if (!isset($bds->id)) {
        $bds->giaodich=0;
        $bds->sonha="";
        $bds->tenduong="";
        $bds->matduong=0;
        $bds->province_id=0;
        $bds->province_ward_id=0;
        $bds->name="";
        $bds->col_id=0;
        // giá
        $bds->gia="";
        $bds->gia_type="vnd"; // vnd, usd, sjc
        $bds->gia_cachtinh="dt"; // dt, m2, thang
        // môi giới
        $bds->moigioi=0; // =0; =1
        $bds->moigioi_chiphi=0; // =0; =1
        // dien tich su dng
        $bds->dtsd=0;
        $bds->dtkv_dai=0;
        $bds->dtkv_rong=0;
        $bds->dtkv_nohau=0;
        $bds->dtkv_nohaum=0;
        
        $bds->dtxd_dai=0;
        $bds->dtxd_rong=0;
        $bds->dtxd_nohau=0;
        $bds->dtxd_nohaum=0;
        
        // đặc điểm
        $bds->phaply=0;
        $bds->huong=0;
        $bds->duongtruocnha=0;
        $bds->solau=0;
        $bds->sophongkhach=0;
        $bds->sophongngu=0;
        $bds->sowc=0;
        $bds->sophongkhac=0;
        
        // tiện ích khác
        $bds->tienich="000000000";
        
        // mô tả chi tiết bất động sản
        $bds->name="";
        $bds->mota="";
        
        // vị trí
        $bds->vitri_lat="14.058324";
        $bds->vitri_long="108.277199";
        $bds->vitri_zoom=7;
        
        // liên hệ
        if (exponent_users_isLoggedIn())
        {
            $bds->lienhe_ten=trim($user->firstname." ".$user->lastname);
            $bds->lienhe_didong=$user->user_address->denomination;;
        }
        else
        {
            $bds->lienhe_ten="";
            $bds->lienhe_didong="";
        }
        
        $bds->lienhe_dienthoai="";
        $bds->lienhe_diachi="";
        $bds->lienhe_ghichu="";
        
        // for admin
        $bds->ngaydang=time();
        $bds->hethan=strtotime("+7 days");
        $bds->bds_trangthai=0;
        $bds->bds_noibat=0;
        //$bds->bds_dambao=0; // we dont need to calculate dambao, just set as giaodich
        $bds->approve_reason="";
        
        // hình ảnh
        $bds->hinhanh="";
        
    } 

    $config = $db->selectObject('bdsmodule_config',"location_data='".serialize($loc)."'");

    if ($config == null) {
        $config->image_col=0;
    }

    if (exponent_users_isLoggedIn()) {
        // We could not build form in normal way
        //$form = bds_bds::form($bds,$config, $loc);
        //$form->location($loc);
        //$form->meta('action','save');

        // loai tin
        $giaodich=_getCombobox($lang,'giaodich');
        // nhóm
        $cols=array();
        $temp=serialize($loc);
        if ($have_permission)
        {
            $temp=$db->selectObjects("bds_collections"," location_data = '{$temp}' ORDER BY col_type, rank");

            foreach ($temp as $k=>$v)
            {
                if ($v->col_type == 0)
                    $v->name="Địa ốc tự do - ". $v->name;
                else
                    $v->name="Đảm bảo - ". $v->name;

                $cols[$v->id]=$v->name;
            }
        }
        else
        {
            $cols=$db->selectDropdown("bds_collections","name"," (col_type = 0) AND location_data = '{$temp}'");
        }
        
        // mặt đường
        $matduong=_getCombobox($lang,'matduong');
        
        // tỉnh
        $provinces=$db->selectObjects("provinces");
        // quận
        $wards=array();
        if (isset($bds->province_id) && ($bds->province_id > 0))
            $wards=$db->selectObjects("provinces_wards", "pid = {$bds->province_id}");
        else
        {
            // we get all ward in first provinces 
            $temp=null;
            foreach ($provinces as $k=>$v)
            {
                $temp=$v;
                break;
            }
            if ($temp != null)
            {
               $wards=$db->selectObjects("provinces_wards", "pid = {$temp->id}"); 
            }
        }
        
        // pháp lý
        $phaply=_getCombobox($lang,'phaply');
        
        // hướng
        $huong=_getCombobox($lang,'huong');
        
        // đường trước nhà
        $duongtruocnha=_getCombobox($lang,'duongtruocnha');
        
        // số lầu - phòng
        $solau=_getComboboxLinear(70);
        $sophongkhac=_getComboboxLinear(15);
        $sophongngu=_getComboboxLinear(70);
        $sophongkhach=_getComboboxLinear(30);
        $sowc=_getComboboxLinear(70);
        
        // tiện ích
        $tienich=_getCombobox($lang,'tienich');
        
        // ngày đăng, hết hạn
        if($bds->ngaydang == time())
            $bds->ngaydang_full=date("r",$bds->ngaydang);
        else
            $bds->ngaydang_full=_ab_dateFormat($bds->ngaydang);
        $bds->hethan_full=_ab_dateFormat($bds->hethan);
        
        // trạng thái
        $trangthai=_getCombobox($lang,"trangthai");
        
        // template
        $template = new template('bdsmodule','_form_edit', $loc);
        $template->assign('config',$config);
        $template->assign('is_edit',(isset($bds->id)? 1 : 0));
        $template->assign('bds',$bds);
        $template->assign('giaodich',$giaodich);
        $template->assign('collections',$cols);
        $template->assign('matduong',$matduong);
        $template->assign('provinces',$provinces);
        $template->assign('wards',$wards);
        $template->assign('phaply',$phaply);
        $template->assign('huong',$huong);
        $template->assign('duongtruocnha',$duongtruocnha);
        $template->assign('solau',$solau);
        $template->assign('sophongkhach',$sophongkhach);
        $template->assign('sowc',$sowc);
        $template->assign('sophongngu',$sophongngu);
        $template->assign('sophongkhac',$sophongkhac);
        $template->assign('tienich',$tienich);
        $template->assign('trangthai',$trangthai);
        $template->assign('user',$user);
        
        // permission
        $have_permission=false;
        if (exponent_users_isAdmin() || exponent_permissions_check('administrate',$loc) || exponent_permissions_check('create',$loc) || exponent_permissions_check('edit',$loc))
        {
            $have_permission=true;
        }
        $template->assign('have_permission',$have_permission);
        
        $template->output();
    } else {
        exponent_flow_redirecto_login();
    }

?>